﻿using Grpc.Core;
using GRPCClass;
using SdtpsLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GRPCServer
{
   public class gRPCImpl  : GRPCClass.gRPC.gRPCBase
    {
        public override Task<TaskReply> SayHello(TaskRequest request, ServerCallContext context)
        {

            return Task.FromResult(new TaskReply { Message = "Hello " + request.Name,TaskInfo=request });
        }
        /// <summary>
        /// 添加一个任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> AddTask(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            mytaskr.Message = "";
            string msg;
            if (TaskHanle.AddTask(request, out msg))
            {
                mytaskr.Status = 5;
                mytaskr.Message = msg;
                return Task.FromResult(mytaskr);
            }
            mytaskr.Message = msg;
            mytaskr.Status = 401;//提交失败

            return Task.FromResult(mytaskr);
        }
        public override Task<TaskReply> GetTaskNoEndCount(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString();
            mytaskr.Message= TaskHanle.GetTaskNoEndCount().ToString();
            
            return Task.FromResult(mytaskr); 
        }
        public override Task<TaskReply> GetTaskNoAllotCount(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString();
            mytaskr.Message = TaskHanle.GetTaskNoAllotCount().ToString();

            return Task.FromResult(mytaskr);

        }
        /// <summary>
        /// 注册一个新的任务执行节点（心跳反馈)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> EnrollNode(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString();
           // mytaskr.Message = "注册一个新的执行节点";
            mytaskr.Message= TaskHanle.EnrollNode(request);
            return Task.FromResult(mytaskr);
        }
        /// <summary>
        /// 注销一个任务执行节点
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> LogoutNode(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString();
            mytaskr.Message = TaskHanle.GetTaskNoAllotCount().ToString();
            
            return Task.FromResult(mytaskr);
        }
        /// <summary>
        /// 执行节点反馈任务完成情况
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> ReplyNode(TaskReply vtaskreply, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = vtaskreply.TaskInfo;
            mytaskr.Message= TaskHanle.ReplyNode(vtaskreply);
            return Task.FromResult(mytaskr);
        }
        /// <summary>
        ///  执行节点 接收任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> AllotTask(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString(); 
            int res = SdtpsNodeLibrary.TaskNodeHandle.AllotTask(request);
            mytaskr.Message = res.ToString();
            return Task.FromResult(mytaskr);
        }
        /// <summary>
        /// 反馈任务执行状态 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task<TaskReply> QueryTask(TaskRequest request, ServerCallContext context)
        {
            TaskReply mytaskr = new TaskReply();
            mytaskr.TaskInfo = request;
            request.SendTime = DateTime.Now.ToString();
            mytaskr.Message = TaskHanle.GetTaskNoAllotCount().ToString();

            return Task.FromResult(mytaskr);
        }

        //public   Task<TaskReply> SayHello(TaskRequest request, ServerCallContext context)
        //{

        //    return Task.FromResult(new TaskReply { Message = "Hello " + request.Name });
        //}

    }
}
